package com.rich.dmr.utils;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import com.rich.dmr.model.Frequency;

import java.util.ArrayList;
import java.util.List;

public class DataBaseUtils {
    private static DataBaseUtils mDataBaseUtils;
    private ContentResolver mContentResolver;
    Uri frequencyUri = Uri.parse("content://com.provider.dmr.app/frequency");

    public static DataBaseUtils getInstance(Context context) {
        if (mDataBaseUtils == null) {
            synchronized (DataBaseUtils.class) {
                mDataBaseUtils = new DataBaseUtils(context);
            }
        }
        return mDataBaseUtils;
    }
    private DataBaseUtils(Context context) {
        mContentResolver = context.getContentResolver();
    }

    public boolean insertChannel(int channel, int txFre, int rxFre){
        ContentValues values = new ContentValues();
        values.put("channel", channel);
        values.put("tx_frequency", txFre);
        values.put("rx_frequency", rxFre);
        Uri uri = mContentResolver.insert(frequencyUri, values);
        assert uri != null;
        String url = uri.toString();
        String insertUrl = url.substring(url.length() - 2);
        return insertUrl.equals("ok");
    }

    public List<Frequency> findFrequencyList() {
        List<Frequency> list = new ArrayList<>();
        Cursor cursor = mContentResolver.query(frequencyUri, null, null, null, "channel asc");
        if (cursor != null) {
            while (cursor.moveToNext()) {
                int channel = cursor.getInt(cursor.getColumnIndex("channel"));
                int txFrequency = cursor.getInt(cursor.getColumnIndex("tx_frequency"));
                int rxFrequency = cursor.getInt(cursor.getColumnIndex("rx_frequency"));
                list.add(new Frequency(channel,txFrequency, rxFrequency));
            }
            cursor.close();
        }
        return list;
    }

    public Frequency findFrequencyByFre(int txFre,int rxFre) {
        Cursor cursor = mContentResolver.query(frequencyUri, null, "tx_frequency=? and rx_frequency=?", new String[]{String.valueOf(txFre),String.valueOf(rxFre)}, "channel asc");
        Frequency frequency = null;
        if (cursor != null) {
            while (cursor.moveToNext()) {
                int channel = cursor.getInt(cursor.getColumnIndex("channel"));
                int txFrequency = cursor.getInt(cursor.getColumnIndex("tx_frequency"));
                int rxFrequency = cursor.getInt(cursor.getColumnIndex("rx_frequency"));
                frequency = new Frequency(channel,txFrequency, rxFrequency);
            }
            cursor.close();
        }
        return frequency;
    }

    public boolean deleteChannel(){
        int res = mContentResolver.delete(frequencyUri,null,null);
        return res == 0;
    }

    public boolean deleteChannelByChannel(int channel){
        int res = mContentResolver.delete(frequencyUri,"channel=?",new String[]{String.valueOf(channel)});
        return res == 0;
    }
}
